package gui_classes;

import java_persistence.NurseMapper;
import java_persistence.PatientMapper;
import java_persistence.UserMapper;

import javax.swing.*;

import org.apache.ibatis.session.SqlSession;

import domain.Doctor;
import domain.Nurse;
import domain.Patient;
import domain.SystemAdministrator;
import domain.User;

import java.awt.*;
import java.awt.event.*;

/**
 * Is a panel for adding a new patient
 */
public class AddUserPanel extends AddPatientPanel {
	private JComboBox comboBox;

	/**
	 * Create the panel.
	 */
	public AddUserPanel() {
		super();
		super.removeButton();
		GUI_Main.getFrame().setTitle("Adding A New User");
		
		getTextField2().setText("(Only fill if user is Patient)");
		getTextField3().setText("(Only fill if user is a Patient)");
		setPreferredSize(new Dimension(600, 400));
		
		JLabel lblUserType = new JLabel("User Type:");
		lblUserType.setBounds(12, 361, 89, 15);
		add(lblUserType);
		
		String[] uStr = {"Patient", "Nurse", "Doctor", "System Administrator"};
		comboBox = new JComboBox(uStr);
		comboBox.setBounds(103, 356, 94, 24);
		add(comboBox);
		
		JButton btnAddUser = new JButton("Add User");
		btnAddUser.setBounds(309, 356, 117, 25);
		btnAddUser.addActionListener(new AddUserListener());
		add(btnAddUser);


	}
	
	/**
	 * Returns the combobox indicating user type
	 */
	public JComboBox getComboBox()
	{
		return this.comboBox;
	}
	
	public void display()
	{
		GUI_Main.replacePanel(GUI_Main.adduserPage);
	}
	
	/**
	 * private listener class for Add Button
	 */
	private class AddUserListener implements ActionListener {
		public void actionPerformed(ActionEvent event){
			
					
			//have data checks right here
						
			try
			{
				
				String[] myArr = new String[11];
				
				myArr[0] =  GUI_Main.adduserPage.getUsername().getText(); // username
				myArr[1] = GUI_Main.adduserPage.getPassword().getText(); //password
				String fName = GUI_Main.adduserPage.getNameF().getText() + " " + GUI_Main.adduserPage.getLName().getText(); 
				myArr[2] = fName; //Name
				myArr[3] = GUI_Main.adduserPage.getPhN().getText(); //phone number
				myArr[4] = GUI_Main.adduserPage.getAddr().getText(); //address
				myArr[5] = GUI_Main.adduserPage.getTextField().getText(); //email
				
				// See what type of user this is
				if(((String)comboBox.getSelectedItem()).equals("Patient"))
				{
					// Adding a new Patient
					System.out.println("Adding a Patient");
					
					myArr[6] = GUI_Main.adduserPage.getTxtGender().getText(); // gender
					String bdate = GUI_Main.adduserPage.getYear().getText() + "-"+GUI_Main.adduserPage.getMonth().getText()+"-"+GUI_Main.adduserPage.getDay().getText();
					myArr[7] = bdate; // birthdate
					myArr[8] = GUI_Main.adduserPage.getTextField1().getText(); //allergies
					myArr[9] = GUI_Main.adduserPage.getTextField2().getText(); // insurance
					myArr[10] = GUI_Main.adduserPage.getTextField3().getText(); // pharmacy
					
					Patient new_patient = new Patient();
					
					new_patient.setUsername(myArr[0]);
					new_patient.setPassword(myArr[1]);
					new_patient.setName(myArr[2]);
					new_patient.setPhone_number(myArr[3]);
					new_patient.setAddress(myArr[4]);
					new_patient.setEmail(myArr[5]);

					new_patient.setIs_male( (myArr[6].equalsIgnoreCase("M"))? true:false );  // once again, if "gender" isn't exactly M, it'll be a female.
					new_patient.setBirthdate(myArr[7]);
					new_patient.setAllergies(myArr[8]);
					new_patient.setInsurance_info(myArr[9]);
					new_patient.setPharmacy(myArr[10]);
					
					new_patient.create(GUI_Main.getSession().getMapper(UserMapper.class));
					new_patient.create_patient(GUI_Main.getSession().getMapper(PatientMapper.class));
					GUI_Main.getSession().commit();
										
					
		//			new_user.create(session.getMapper(UserMapper.class));
		//			session.commit();
				}
				if(((String)comboBox.getSelectedItem()).equals("Nurse"))
				{
					// Adding a new Nurse
					System.out.println("Adding a Nurse");
					Nurse new_nurse = new Nurse();
					
					new_nurse.setUsername(myArr[0]);
					new_nurse.setPassword(myArr[1]);
					new_nurse.setName(myArr[2]);
					new_nurse.setPhone_number(myArr[3]);
					new_nurse.setAddress(myArr[4]);
					new_nurse.setEmail(myArr[5]);
					
					new_nurse.setActive_account(true);
					
					new_nurse.create(GUI_Main.getSession().getMapper(UserMapper.class));
					GUI_Main.getSession().commit();
				}
				if(((String)comboBox.getSelectedItem()).equals("Doctor"))
				{
					// Adding a new Doctor
					System.out.println("Adding a Doctor");
					Doctor new_doctor = new Doctor();
					
					new_doctor.setUsername(myArr[0]);
					new_doctor.setPassword(myArr[1]);
					new_doctor.setName(myArr[2]);
					new_doctor.setPhone_number(myArr[3]);
					new_doctor.setAddress(myArr[4]);
					new_doctor.setEmail(myArr[5]);
					
					new_doctor.setActive_account(true);
					
					new_doctor.create(GUI_Main.getSession().getMapper(UserMapper.class));
					GUI_Main.getSession().commit();
				}
				if(((String)comboBox.getSelectedItem()).equals("System Administrator"))
				{
					// Adding a new Syste Administrator
					System.out.println("Adding a System Administrator");
					SystemAdministrator new_sa = new SystemAdministrator();
					
					new_sa.setUsername(myArr[0]);
					new_sa.setPassword(myArr[1]);
					new_sa.setName(myArr[2]);
					new_sa.setPhone_number(myArr[3]);
					new_sa.setAddress(myArr[4]);
					new_sa.setEmail(myArr[5]);
					
					new_sa.setActive_account(true);
					
					new_sa.create(GUI_Main.getSession().getMapper(UserMapper.class));
					GUI_Main.getSession().commit();
				}
				
				
				
		/*		String[] myArr = new String[11];
				
				myArr[0] =  GUI_Main.adduserPage.getUsername().getText();
				myArr[1] = GUI_Main.adduserPage.getPassword().getText(); //password
				String fName = GUI_Main.adduserPage.getNameF().getText() + " " + GUI_Main.adduserPage.getLName().getText(); 
				myArr[2] = fName; //Name
				myArr[3] = GUI_Main.adduserPage.getPhN().getText(); //phone number
				myArr[4] = GUI_Main.adduserPage.getAddr().getText(); //address
				myArr[5] = GUI_Main.adduserPage.getTextField().getText(); //email
				
				User new_user = new User();
				new_user.setUsername(myArr[0]);
				new_user.setPassword(myArr[1]);
				new_user.setName(myArr[2]);
				new_user.setPhone_number(myArr[3]);
				new_user.setAddress(myArr[4]);
				new_user.setEmail(myArr[5]);
				
				SqlSession session = GUI_Main.getSession();
				try{ 
					new_user.create(session.getMapper(UserMapper.class));
					session.commit();
				} catch (Exception e) {
					/*
					 * Pratik: we should probably be displaying errors to the user in these.
					 */
					
				//	System.out.println("Database Error: " + e.getMessage());
		/*		} 
				

				if( ((String)(GUI_Main.adduserPage.getComboBox().getSelectedItem())).equals("Patient")  )
				{
					Patient new_patient = (Patient) new_user;
					
					
					myArr[6] = GUI_Main.adduserPage.getTxtGender().getText(); // gender
					String bdate = GUI_Main.adduserPage.getYear().getText() + "-"+GUI_Main.adduserPage.getMonth().getText()+"-"+GUI_Main.adduserPage.getDay().getText();
					myArr[7] = bdate; // birthdate
					myArr[8] = GUI_Main.adduserPage.getTextField1().getText(); //allergies
					myArr[9] = GUI_Main.adduserPage.getTextField2().getText(); // insurance
					myArr[10] = GUI_Main.adduserPage.getTextField3().getText(); // pharmacy
					
					new_patient.setIs_male( (myArr[6].equals("M"))? true:false );  // once again, if "gender" isn't exactly M, it'll be a female.
					new_patient.setBirthdate(myArr[7]);
					new_patient.setAllergies(myArr[8]);
					new_patient.setInsurance_info(myArr[9]);
					new_patient.setPharmacy(myArr[10]);
					
					try {
						new_patient.create_patient(session.getMapper(PatientMapper.class));
						session.commit();
					}
					catch (Exception e) {
						System.out.println("Database Error: " + e.getMessage());
					} 
					
				} */
				
				
				
		/*		GUI_Main.adduserPage.setArray(myArr); */
			
			
				/* -Joseph:  commit new user to database ( if there is no problem within registration info 
				 * GUI_Main.rpage.getArray()[0] has username.. and so on.
				 * */

				/*
				 * Pratik: See above.
				 * 
				 * I did it in 2 stages because it wasn't immediately clear if the user was a patient or not.
				 * Fortunately, the db can handle that just fine.
				 * 
				 * If it's possible to register other types of users in this method, please use similar logic as above and leave me a note.
				 * 
				 */
				
				
				
				
				
				//GUI_Main.replacePanel(GUI_Main.lpanel);
			}
			catch( org.apache.ibatis.exceptions.PersistenceException e)
			{
				JOptionPane.showMessageDialog(null, "Human: One of your text entries is too large \nProgrammer:" + e.getMessage());
				

			}
			catch(NullPointerException e)
			{
				JOptionPane.showMessageDialog(null, "You have not filled in all the data. Try again.");
			} 
				
		}
				
			
			
			
	}
}
